﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Data;
using System.ComponentModel;
namespace WPFExcelReport
{
    public partial class TSWFBOOK : ViewBase
    {
        #region ==WFEVENTS===========
        public static readonly DependencyProperty WFEVENTSProperty = DependencyProperty.Register("WFEVENTS",
            typeof(string), typeof(TSWFBOOK),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnWFEVENTSChanged));
        public string WFEVENTS
        {
            get
            {
                return (string)GetValue(WFEVENTSProperty);
            }
            set
            {
                SetValue(WFEVENTSProperty, value);
                Locate();
            }
        }
        private static void OnWFEVENTSChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TSWFBOOK;
            if (src != null)
            {
                src.WFEVENTS = (string)e.NewValue;
               
            }
        }
        #endregion
        #region ==dwWFG===========
        public static readonly DependencyProperty dwWFGProperty = DependencyProperty.Register("dwWFG",
            typeof(DataRow), typeof(TSWFBOOK),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OdwWFGChanged));
        public DataRow dwWFG
        {
            get
            {
                return (DataRow)GetValue(dwWFGProperty);
            }
            set
            {
                SetValue(dwWFGProperty, value);
            }
        }
        private static void OdwWFGChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TSWFBOOK;
            if (src != null)
            {
                DataRow row= (DataRow)e.NewValue;
                src.dwWFG = row; 
            }
        }
        #endregion
       
        public TSWFBOOK()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return; 
            MV = new VICC_WFSUBSCRIBER();
            this.DataContext = MV;
            InitializeComponent(); 
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            dwWFG = dwMaster; 
        } 
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            base.Window_Loaded(sender, e);
            Load();
        }
        public void Load()
        {
            SResult = ((VICC_WFSUBSCRIBER)MV).QueryICC_WFSUBSCRIBER(dwData, 9999);
            MergeData(SResult,false); 
        }
        public void Locate()
        {
            if (TBMain == null)
                return;
            int idx = 0;
            var query = from DataRow row in TBMain.AsEnumerable()
                        where row["WFEVENTS"].ToString().Trim() == WFEVENTS.Trim()
                        select row;
            foreach (DataRow row in query)
            {
                 idx = TBMain.Rows.IndexOf(row);              
                break;
            }
            vwMaster.View.MoveCurrentTo(idx);
        }

        public override void EditCommand(object sender, RoutedEventArgs e)
        {
            if (IsEdit)
            {
                Load();
            }
            if (!IsEdit)
            {
                TWFSUBSCRIBER TWFSUBSCRIBER = new TWFSUBSCRIBER();
                ZoneEdit.Children.Add(TWFSUBSCRIBER); 
            }
            base.EditCommand(sender, e);  
        }
    }
}
